﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;

namespace itChatAdmin
{
    public partial class FormFriendAdmin : Form
    {
        public FormFriendAdmin()
        {
            InitializeComponent();
        }

        String UserType = "";
        String UserID = "";
        String NickName = "";
        String UserName = "";

        private void button19_Click(object sender, EventArgs e)
        {
            Form1.Refresh_Friend();
            Refresh_Friends_List();
        }
        private void Refresh_Friends_List()
        {
            this.listView3.Items.Clear();

            SQLiteCommand mySQLiteCommand = new SQLiteCommand("", Form1.memSQLiteConnection);
            if (this.textBox8.Text.Trim() != "")
            {
                mySQLiteCommand.CommandText = "SELECT * FROM MelaFriends WHERE NickName LIKE '%"+ this.textBox8.Text.Trim() + "%'";
            }
            else
            {
                mySQLiteCommand.CommandText = "SELECT * FROM MelaFriends ORDER BY ID ASC";

            }


            SQLiteDataReader mySQLiteDataReader = mySQLiteCommand.ExecuteReader();
            mySQLiteCommand.Parameters.Clear();
            while (mySQLiteDataReader.Read())
            {
                String ID = mySQLiteDataReader["ID"].ToString().Trim();
                String NickName = mySQLiteDataReader["NickName"].ToString().Trim();
                String IsEnabled = mySQLiteDataReader["IsEnabled"].ToString().Trim();
                int GroupsIndex = 0;
                if (Boolean.Parse(IsEnabled) == true)
                {
                    GroupsIndex = 0;
                }
                else
                {
                    GroupsIndex = 1;
                }

                ListViewItem item = new ListViewItem(new string[] { ID, NickName }, 0, this.listView3.Groups[GroupsIndex]);
                this.Invoke((EventHandler)delegate
                {
                    listView3.Items.Add(item);
                });
            }
            mySQLiteDataReader.Close();

        }
        private void Refresh_Friends(String WxMessage)
        {
            //JObject json = JObject.Parse(result);
            JArray json = JArray.Parse(WxMessage);
            //更新数据库及群列表

            SQLiteCommand mySQLiteCommand = new SQLiteCommand("", Form1.memSQLiteConnection);
            foreach (JObject group in json)
            {
                if (group["NickName"].ToString().Trim() == "") continue;

                mySQLiteCommand.CommandText = "SELECT count(*) FROM MelaFriends WHERE NickName=@NickName";
                mySQLiteCommand.Parameters.Add(new SQLiteParameter("@NickName"));
                mySQLiteCommand.Parameters["@NickName"].Value = group["NickName"].ToString().Trim();

                object i = mySQLiteCommand.ExecuteScalar();
                mySQLiteCommand.Parameters.Clear();

                if (int.Parse(i.ToString().Trim()) > 0)
                {
                    mySQLiteCommand.CommandText = @"UPDATE MelaFriends SET 
UserName=@UserName,
City=@City,
DisplayName=@DisplayName,
UniFriend=@UniFriend,
OwnerUin=@OwnerUin,
MemberList=@MemberList,
PYQuanPin=@PYQuanPin,
RemarkPYInitial=@RemarkPYInitial,
Uin=@Uin,
AppAccountFlag=@AppAccountFlag,
VerifyFlag=@VerifyFlag,
Province=@Province,
KeyWord=@KeyWord,
RemarkName=@RemarkName,
PYInitial=@PYInitial,
ChatRoomId=@ChatRoomId,
HideInputBarFlag=@HideInputBarFlag,
HeadImgFlag=@HeadImgFlag,
EncryChatRoomId=@EncryChatRoomId,
AttrStatus=@AttrStatus,
SnsFlag=@SnsFlag,
MemberCount=@MemberCount,
IsOwner=@IsOwner,
WebWxPluginSwitch=@WebWxPluginSwitch,
Alias=@Alias,
Signature=@Signature,
ContactFlag=@ContactFlag,
RemarkPYQuanPin=@RemarkPYQuanPin,
HeadImgUrl=@HeadImgUrl,
Sex=@Sex,
StarFriend=@StarFriend,
Statues=@Statues
WHERE NickName=@NickName";
                    //UpdateDataTime = @UpdateDataTime

                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@UserName"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@City"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@DisplayName"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@UniFriend"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@OwnerUin"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@MemberList"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@PYQuanPin"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@RemarkPYInitial"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Uin"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@AppAccountFlag"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@VerifyFlag"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Province"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@KeyWord"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@RemarkName"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@PYInitial"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@ChatRoomId"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@HideInputBarFlag"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@HeadImgFlag"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@EncryChatRoomId"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@AttrStatus"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@SnsFlag"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@MemberCount"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@IsOwner"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@WebWxPluginSwitch"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Alias"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Signature"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@ContactFlag"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@RemarkPYQuanPin"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@HeadImgUrl"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Sex"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@StarFriend"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Statues"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@NickName"));



                    mySQLiteCommand.Parameters["@UserName"].Value = group["UserName"].ToString().Trim();
                    mySQLiteCommand.Parameters["@City"].Value = group["City"].ToString().Trim();
                    mySQLiteCommand.Parameters["@DisplayName"].Value = group["DisplayName"].ToString().Trim();
                    mySQLiteCommand.Parameters["@UniFriend"].Value = group["UniFriend"].ToString().Trim();
                    mySQLiteCommand.Parameters["@OwnerUin"].Value = group["OwnerUin"].ToString().Trim();
                    mySQLiteCommand.Parameters["@MemberList"].Value = group["MemberList"].ToString().Trim();
                    mySQLiteCommand.Parameters["@PYQuanPin"].Value = group["PYQuanPin"].ToString().Trim();
                    mySQLiteCommand.Parameters["@RemarkPYInitial"].Value = group["RemarkPYInitial"].ToString().Trim();
                    mySQLiteCommand.Parameters["@Uin"].Value = group["Uin"].ToString().Trim();
                    mySQLiteCommand.Parameters["@AppAccountFlag"].Value = group["AppAccountFlag"].ToString().Trim();
                    mySQLiteCommand.Parameters["@VerifyFlag"].Value = group["VerifyFlag"].ToString().Trim();
                    mySQLiteCommand.Parameters["@Province"].Value = group["Province"].ToString().Trim();
                    mySQLiteCommand.Parameters["@KeyWord"].Value = group["KeyWord"].ToString().Trim();
                    mySQLiteCommand.Parameters["@RemarkName"].Value = group["RemarkName"].ToString().Trim();
                    mySQLiteCommand.Parameters["@PYInitial"].Value = group["PYInitial"].ToString().Trim();
                    mySQLiteCommand.Parameters["@ChatRoomId"].Value = group["ChatRoomId"].ToString().Trim();
                    mySQLiteCommand.Parameters["@HideInputBarFlag"].Value = group["HideInputBarFlag"].ToString().Trim();
                    if (group["HeadImgFlag"] != null)
                    {
                        mySQLiteCommand.Parameters["@HeadImgFlag"].Value = group["HeadImgFlag"].ToString().Trim();
                    }
                    else
                    {
                        mySQLiteCommand.Parameters["@HeadImgFlag"].Value = "";

                    }
                    mySQLiteCommand.Parameters["@EncryChatRoomId"].Value = group["EncryChatRoomId"].ToString().Trim();
                    mySQLiteCommand.Parameters["@AttrStatus"].Value = group["AttrStatus"].ToString().Trim();
                    mySQLiteCommand.Parameters["@SnsFlag"].Value = group["SnsFlag"].ToString().Trim();
                    mySQLiteCommand.Parameters["@MemberCount"].Value = group["MemberCount"].ToString().Trim();

                    if (group["IsOwner"] != null)
                    {
                        mySQLiteCommand.Parameters["@IsOwner"].Value = group["IsOwner"].ToString().Trim();
                    }
                    else
                    {
                        mySQLiteCommand.Parameters["@IsOwner"].Value = "";
                    }

                    if (group["WebWxPluginSwitch"] != null)
                    {
                        mySQLiteCommand.Parameters["@WebWxPluginSwitch"].Value = group["WebWxPluginSwitch"].ToString().Trim();
                    }
                    else
                    {
                        mySQLiteCommand.Parameters["@WebWxPluginSwitch"].Value = "";
                    }
                    mySQLiteCommand.Parameters["@Alias"].Value = group["Alias"].ToString().Trim();
                    mySQLiteCommand.Parameters["@Signature"].Value = group["Signature"].ToString().Trim();
                    mySQLiteCommand.Parameters["@ContactFlag"].Value = group["ContactFlag"].ToString().Trim();
                    mySQLiteCommand.Parameters["@RemarkPYQuanPin"].Value = group["RemarkPYQuanPin"].ToString().Trim();
                    mySQLiteCommand.Parameters["@HeadImgUrl"].Value = group["HeadImgUrl"].ToString().Trim();
                    mySQLiteCommand.Parameters["@Sex"].Value = group["Sex"].ToString().Trim();
                    mySQLiteCommand.Parameters["@StarFriend"].Value = group["StarFriend"].ToString().Trim();
                    mySQLiteCommand.Parameters["@Statues"].Value = group["Statues"].ToString().Trim();
                    mySQLiteCommand.Parameters["@NickName"].Value = group["NickName"].ToString().Trim();
                }
                else
                {
                    mySQLiteCommand.CommandText = @"INSERT INTO MelaFriends(UserName,
City,
DisplayName,
UniFriend,
OwnerUin,
MemberList,
PYQuanPin,
RemarkPYInitial,
Uin,
AppAccountFlag,
VerifyFlag,
Province,
KeyWord,
RemarkName,
PYInitial,
ChatRoomId,
HideInputBarFlag,
HeadImgFlag,
EncryChatRoomId,
AttrStatus,
SnsFlag,
MemberCount,
IsOwner,
WebWxPluginSwitch,
Alias,
Signature,
ContactFlag,
NickName,
RemarkPYQuanPin,
HeadImgUrl,
Sex,
StarFriend,
Statues) VALUES(@UserName,
@City,
@DisplayName,
@UniFriend,
@OwnerUin,
@MemberList,
@PYQuanPin,
@RemarkPYInitial,
@Uin,
@AppAccountFlag,
@VerifyFlag,
@Province,
@KeyWord,
@RemarkName,
@PYInitial,
@ChatRoomId,
@HideInputBarFlag,
@HeadImgFlag,
@EncryChatRoomId,
@AttrStatus,
@SnsFlag,
@MemberCount,
@IsOwner,
@WebWxPluginSwitch,
@Alias,
@Signature,
@ContactFlag,
@NickName,
@RemarkPYQuanPin,
@HeadImgUrl,
@Sex,
@StarFriend,
@Statues
)";
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@UserName"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@City"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@DisplayName"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@UniFriend"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@OwnerUin"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@MemberList"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@PYQuanPin"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@RemarkPYInitial"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Uin"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@AppAccountFlag"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@VerifyFlag"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Province"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@KeyWord"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@RemarkName"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@PYInitial"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@ChatRoomId"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@HideInputBarFlag"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@HeadImgFlag"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@EncryChatRoomId"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@AttrStatus"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@SnsFlag"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@MemberCount"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@IsOwner"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@WebWxPluginSwitch"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Alias"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Signature"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@ContactFlag"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@NickName"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@RemarkPYQuanPin"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@HeadImgUrl"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Sex"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@StarFriend"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Statues"));



                    mySQLiteCommand.Parameters["@UserName"].Value = group["UserName"].ToString().Trim();
                    mySQLiteCommand.Parameters["@City"].Value = group["City"].ToString().Trim();
                    mySQLiteCommand.Parameters["@DisplayName"].Value = group["DisplayName"].ToString().Trim();
                    mySQLiteCommand.Parameters["@UniFriend"].Value = group["UniFriend"].ToString().Trim();
                    mySQLiteCommand.Parameters["@OwnerUin"].Value = group["OwnerUin"].ToString().Trim();
                    mySQLiteCommand.Parameters["@MemberList"].Value = group["MemberList"].ToString().Trim();
                    mySQLiteCommand.Parameters["@PYQuanPin"].Value = group["PYQuanPin"].ToString().Trim();
                    mySQLiteCommand.Parameters["@RemarkPYInitial"].Value = group["RemarkPYInitial"].ToString().Trim();
                    mySQLiteCommand.Parameters["@Uin"].Value = group["Uin"].ToString().Trim();
                    mySQLiteCommand.Parameters["@AppAccountFlag"].Value = group["AppAccountFlag"].ToString().Trim();
                    mySQLiteCommand.Parameters["@VerifyFlag"].Value = group["VerifyFlag"].ToString().Trim();
                    mySQLiteCommand.Parameters["@Province"].Value = group["Province"].ToString().Trim();
                    mySQLiteCommand.Parameters["@KeyWord"].Value = group["KeyWord"].ToString().Trim();
                    mySQLiteCommand.Parameters["@RemarkName"].Value = group["RemarkName"].ToString().Trim();
                    mySQLiteCommand.Parameters["@PYInitial"].Value = group["PYInitial"].ToString().Trim();
                    mySQLiteCommand.Parameters["@ChatRoomId"].Value = group["ChatRoomId"].ToString().Trim();
                    mySQLiteCommand.Parameters["@HideInputBarFlag"].Value = group["HideInputBarFlag"].ToString().Trim();

                    if (group["HeadImgFlag"] != null)
                    {
                        mySQLiteCommand.Parameters["@HeadImgFlag"].Value = group["HeadImgFlag"].ToString().Trim();
                    }
                    else
                    {
                        mySQLiteCommand.Parameters["@HeadImgFlag"].Value = "";

                    }
                    mySQLiteCommand.Parameters["@EncryChatRoomId"].Value = group["EncryChatRoomId"].ToString().Trim();
                    mySQLiteCommand.Parameters["@AttrStatus"].Value = group["AttrStatus"].ToString().Trim();
                    mySQLiteCommand.Parameters["@SnsFlag"].Value = group["SnsFlag"].ToString().Trim();
                    mySQLiteCommand.Parameters["@MemberCount"].Value = group["MemberCount"].ToString().Trim();


                    if (group["IsOwner"] != null)
                    {
                        mySQLiteCommand.Parameters["@IsOwner"].Value = group["IsOwner"].ToString().Trim();

                    }
                    else
                    {
                        mySQLiteCommand.Parameters["@IsOwner"].Value = "";

                    }
                    if (group["WebWxPluginSwitch"] != null)
                    {
                        mySQLiteCommand.Parameters["@WebWxPluginSwitch"].Value = group["WebWxPluginSwitch"].ToString().Trim();

                    }
                    else
                    {
                        mySQLiteCommand.Parameters["@WebWxPluginSwitch"].Value = "";

                    }
                    mySQLiteCommand.Parameters["@Alias"].Value = group["Alias"].ToString().Trim();
                    mySQLiteCommand.Parameters["@Signature"].Value = group["Signature"].ToString().Trim();
                    mySQLiteCommand.Parameters["@ContactFlag"].Value = group["ContactFlag"].ToString().Trim();
                    mySQLiteCommand.Parameters["@NickName"].Value = group["NickName"].ToString().Trim();
                    mySQLiteCommand.Parameters["@RemarkPYQuanPin"].Value = group["RemarkPYQuanPin"].ToString().Trim();
                    mySQLiteCommand.Parameters["@HeadImgUrl"].Value = group["HeadImgUrl"].ToString().Trim();
                    mySQLiteCommand.Parameters["@Sex"].Value = group["Sex"].ToString().Trim();
                    mySQLiteCommand.Parameters["@StarFriend"].Value = group["StarFriend"].ToString().Trim();
                    mySQLiteCommand.Parameters["@Statues"].Value = group["Statues"].ToString().Trim();
                }
                mySQLiteCommand.ExecuteNonQuery();
                Form1.WriteToDBInDisk(mySQLiteCommand);
                mySQLiteCommand.Parameters.Clear();
            }

        }
        private String SendCommnadTo_itChat(Object command)
        {
            try
            {
                String sendString = new JavaScriptSerializer().Serialize(command);
                WebClient wc = new WebClient();
                StringBuilder postData = new StringBuilder();
                postData.Append(sendString);
                byte[] sendData = Encoding.GetEncoding("utf-8").GetBytes(postData.ToString());
                wc.Headers.Add("Content-Type", "application/json");
                wc.Headers.Add("ContentLength", sendData.Length.ToString());
                byte[] recData = wc.UploadData("http://"+AppConfig.GetAppSettingValue("itChatMasterIP")+":" + AppConfig.GetAppSettingValue("itChatMasterPort") + "/", sendData);
                return System.Web.HttpUtility.UrlDecode(Encoding.GetEncoding("utf-8").GetString(recData).Replace(@"\u", "%u"));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return "";
            }
        }
        private void button20_Click(object sender, EventArgs e)
        {
            //查找 联系人
            Refresh_Friends_List();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            //发送一条测试信息

            String Msg =Form1.GetOneMelaMessages(this.textBox9.Text);
            if (Msg == "")
            {
                MessageBox.Show("没有查到发送的数据");
                return;
            }
            Object command = new
            {
                cmd = WX_COMMAND.send_msg.ToString(),
                data_0 = Msg,
                data_1 = UserName,
                data_2 = "",
                data_3 = ""

            };
            //向服务器发送数据
            String result = SendCommnadTo_itChat(command);
            Form1.InsetIntoLogs(UserType, NickName, Msg, result);
            Form1.Write_Log(result, WX_COMMAND.send_msg.ToString(), 4);
            RefreshFriendsLogs();
        }
        private void RefreshFriendsLogs()
        {
            listView4.Items.Clear();
            this.textBox9.Text = "";
            this.textBox14.Text = "";

            SQLiteCommand mySQLiteCommand = new SQLiteCommand("", Form1.memSQLiteConnection);
            mySQLiteCommand.CommandText = "SELECT * FROM MelaSendMsgLog WHERE UserType=@UserType AND NickName=@NickName";
            mySQLiteCommand.Parameters.Add(new SQLiteParameter("@UserType"));
            mySQLiteCommand.Parameters.Add(new SQLiteParameter("@NickName"));
            mySQLiteCommand.Parameters["@UserType"].Value = UserType;
            mySQLiteCommand.Parameters["@NickName"].Value = NickName;
            SQLiteDataReader mySQLiteDataReader3 = mySQLiteCommand.ExecuteReader();
            mySQLiteCommand.Parameters.Clear();

            while (mySQLiteDataReader3.Read())
            {
                String ID = mySQLiteDataReader3["ID"].ToString().Trim();
                String SendDateTime = mySQLiteDataReader3["UpdateDataTime"].ToString().Trim();
                String MsgID = mySQLiteDataReader3["SendMsg"].ToString().Trim();
                String SendResult = mySQLiteDataReader3["SendResult"].ToString().Trim();

                ListViewItem item = new ListViewItem(new string[] { ID, SendDateTime, MsgID, SendResult });

                this.Invoke((EventHandler)delegate
                {
                    listView4.Items.Add(item);
                });
            }
            mySQLiteDataReader3.Close();


            //更新右键选项
            mySQLiteCommand.CommandText = "SELECT * FROM MelaFriends WHERE NickName=@NickName";
            mySQLiteCommand.Parameters.Add(new SQLiteParameter("@NickName"));
            mySQLiteCommand.Parameters["@NickName"].Value = NickName;
            SQLiteDataReader mySQLiteDataReader1 = mySQLiteCommand.ExecuteReader();
            if (mySQLiteDataReader1.Read())
            {
                Boolean IsEnabled = Boolean.Parse(mySQLiteDataReader1["IsEnabled"].ToString().Trim());
                Boolean IsSkipOneDay = Boolean.Parse(mySQLiteDataReader1["IsSkipOneDay"].ToString().Trim());
                String NickName = mySQLiteDataReader1["NickName"].ToString().Trim();
                String MsgClass = mySQLiteDataReader1["MsgClass"].ToString().Trim();
                String SendTimes = mySQLiteDataReader1["SendTimes"].ToString().Trim();

                启用发送ToolStripMenuItem1.Checked = IsEnabled;

                //过滤陌生人ToolStripMenuItem.Checked = IsGetStrangers;

                this.checkBox2.Checked = IsEnabled;
                this.checkBox4.Checked = IsSkipOneDay;
                this.label21.Text = NickName;
                this.textBox9.Text = MsgClass;
                this.textBox14.Text = SendTimes;

                //更新选择框
                string[] msg = MsgClass.Split('|');

                for (int j = 0; j < this.checkedListBox2.Items.Count; j++)
                {
                    this.checkedListBox2.SetItemChecked(j, false);
                }

                foreach (String m in msg)
                {
                    if (m == "") continue;
                    for (int j = 0; j < checkedListBox2.Items.Count; j++)
                    {
                        if (checkedListBox2.Items[j].ToString() == m)
                        {
                            checkedListBox2.SetItemChecked(j, true);
                        }
                    }
                }
            }
            mySQLiteDataReader1.Close();

            //更新未来发送的计划
            this.listView1.Items.Clear();
            int i = 0;
            foreach(DataRow row in Form1.MsgSendTable.Select("", "SendTime asc"))
            {
                if(row["UserName"].ToString().Trim()!= UserName)
                {
                    continue;
                }
                i++;
                ListViewItem item = new ListViewItem(new string[] {
                    i.ToString().Trim(),
                    row["SendTime"].ToString().Trim(),
                    row["MsgClass"].ToString().Trim(),
                    });

                if (Boolean.Parse(row["IsFinished"].ToString().Trim())==true)
                {
                    item.BackColor = Color.LightGray;
                }

                this.Invoke((EventHandler)delegate
                {
                    listView1.Items.Add(item);
                });
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            SQLiteCommand mySQLiteCommand = new SQLiteCommand("", Form1.memSQLiteConnection);
            int IsEnabled = 0;
            int IsSkipOneDay = 0;
            if (this.checkBox2.Checked)
            {
                IsEnabled = 1;
            }
            if (this.checkBox4.Checked)
            {
                IsSkipOneDay = 1;
            }
            mySQLiteCommand.CommandText = "UPDATE MelaFriends SET IsEnabled='" + IsEnabled + "',IsSkipOneDay='" + IsSkipOneDay + "',MsgClass='" + this.textBox9.Text + "',SendTimes='" + this.textBox14.Text + "' WHERE ID='" + UserID + "'";
            mySQLiteCommand.ExecuteNonQuery();
            Form1.WriteToDBInDisk(mySQLiteCommand);

            //刷新
            MessageBox.Show("保存成功！");
        }

        private void listView3_SelectedIndexChanged(object sender, EventArgs e)
        {
            UserType = "1";
            foreach(ListViewItem lv in this.listView3.Items)
            {
                lv.BackColor = Color.White;
            }

            if (listView3.SelectedIndices != null && listView3.SelectedIndices.Count > 0)
            {
                ListView.SelectedIndexCollection c = listView3.SelectedIndices;
                listView3.Items[c[0]].BackColor = Color.Silver;
                UserID = listView3.Items[c[0]].Text;
                NickName = listView3.Items[c[0]].SubItems[1].Text;
                UserName = GetFirendUserNameWithID(UserID);
                RefreshFriendsLogs();
            }
        }
        private String GetFirendUserNameWithID(String ID)
        {
            SQLiteCommand mySQLiteCommand = new SQLiteCommand("", Form1.memSQLiteConnection);
            mySQLiteCommand.CommandText = "SELECT UserName FROM MelaFriends WHERE ID=" + ID;
            SQLiteDataReader mySQLiteDataReader = mySQLiteCommand.ExecuteReader();
            if (mySQLiteDataReader.Read())
            {
                return mySQLiteDataReader["UserName"].ToString().Trim();
            }
            else
            {
                return "";
            }
        }
        private void listView4_DoubleClick(object sender, EventArgs e)
        {
            if (listView4.SelectedIndices != null && listView4.SelectedIndices.Count > 0)
            {
                ListView.SelectedIndexCollection c = listView4.SelectedIndices;
                String ID = listView4.Items[c[0]].SubItems[0].Text;
                FormLogView fl = new FormLogView(ID);
                fl.ShowDialog();
            }
        }

        private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string MsgClassValue = "";
            foreach (var a in checkedListBox2.CheckedItems)
            {
                MsgClassValue += a.ToString() + "|";
            }

            if (MsgClassValue != "")
            {
                this.textBox9.Text = MsgClassValue.Substring(0, MsgClassValue.Length - 1);
            }
        }

        private void textBox9_Click(object sender, EventArgs e)
        {
            if (this.checkedListBox2.Visible)
            {
                //添加变量
                this.textBox9.Text = "";
                string MsgClassValue = "";
                foreach (var a in checkedListBox2.CheckedItems)
                {
                    MsgClassValue += a.ToString() + "|";
                }

                if (MsgClassValue != "")
                {
                    this.textBox9.Text = MsgClassValue.Substring(0, MsgClassValue.Length - 1);
                }
                this.checkedListBox2.Visible = false;
            }
            else
            {
                this.checkedListBox2.Visible = true;
            }
        }

        private void 启用发送ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (listView3.SelectedIndices != null && listView3.SelectedIndices.Count > 0)
            {
                ListView.SelectedIndexCollection c = listView3.SelectedIndices;
                String ItemIndex = listView3.Items[c[0]].Text;
                Update_MelaFriend(ItemIndex, "IsEnabled");
                Refresh_Friends_List();
                RefreshFriendsLogs();
            }
        }
        private void Update_MelaFriend(String ID, String Clo)
        {
            SQLiteCommand mySQLiteCommand = new SQLiteCommand("", Form1.memSQLiteConnection);
            mySQLiteCommand.CommandText = "update MelaFriends set " + Clo + "=(not " + Clo + ") where id=" + ID;
            mySQLiteCommand.ExecuteNonQuery();
            Form1.WriteToDBInDisk(mySQLiteCommand);
        }

        private void 查看详情ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void FormFriendAdmin_Load(object sender, EventArgs e)
        {
            Refresh_Friends_List();

            this.checkedListBox2.Items.Clear();
            SQLiteCommand mySQLiteCommand = new SQLiteCommand("", Form1.memSQLiteConnection);
            mySQLiteCommand.CommandText = "SELECT * FROM MelaMessagesClass ORDER BY ClassName";
            SQLiteDataReader mySQLiteDataReader1 = mySQLiteCommand.ExecuteReader();
            while (mySQLiteDataReader1.Read())
            {
                this.checkedListBox2.Items.Add(mySQLiteDataReader1["ClassName"].ToString().Trim());
            }
            mySQLiteCommand.Parameters.Clear();
            mySQLiteDataReader1.Close();


        }
    }
}
